package MONITORING::DB::ORACLE::BACKUP_JOB_DETAILS;
 
#################################################################
# $LastChangedDate: 2013-11-25 15:03:06 +0000 (Mon, 25 Nov 2013) $
# $LastChangedRevision: 22 $
# $LastChangedBy: aswod_83@gmx.net $
#################################################################
 
use strict;
use base qw(moniCA::DBH::Oracle);
use XML::Simple;
use Log::Log4perl;

#-------------------------------------------------------------------------------
sub Run {
#-------------------------------------------------------------------------------
# Description:  Comparison tablespace size with config values (failPct,warnPct)
# Input Parms:
# Output Parms: None
#-------------------------------------------------------------------------------	
        my $self        = shift;
        my $cnf         = $self->GetCNF();
        my $logger      = $self->GetLogger();
        my $statefile   = $self->TmpDir() . '/' . $self->Plug() . '.xml';

        my %row=$self->_getVrman_backup_job_details();
		if (! %row){
       		$logger->error("Keine Sicherung gelaufen");
		} else {
        	for my $col1 (keys %row) {
                for my $id (sort keys %{$row{$col1}} ){
					Log::Log4perl::MDC->put('identifier', $row{$col1}{$id}{status});
					if ($row{$col1}{$id}{status} eq "FAILED"){
						$logger->error("start:'$row{$col1}{$id}{start}' duration:'$row{$col1}{$id}{duration}' status:'$row{$col1}{$id}{status}' type:'$row{$col1}{$id}{type}' input:'$row{$col1}{$id}{input}' output:'$row{$col1}{$id}{output}' device:'$row{$col1}{$id}{device}' rate:'$row{$col1}{$id}{rate}'");
					}elsif($row{$col1}{$id}{status} eq ("COMPLETED WITH WARNINGS"||"RUNNING WITH WARNINGS"||"COMPLETED WITH ERRORS"||"RUNNING WITH ERRORS")){
						$logger->warn("start:'$row{$col1}{$id}{start}' duration:'$row{$col1}{$id}{duration}' status:'$row{$col1}{$id}{status}' type:'$row{$col1}{$id}{type}' input:'$row{$col1}{$id}{input}' output:'$row{$col1}{$id}{output}' device:'$row{$col1}{$id}{device}' rate:'$row{$col1}{$id}{rate}'");
					}elsif($row{$col1}{$id}{status} eq ("COMPLETED"||"RUNNING")){
						$logger->info("start:'$row{$col1}{$id}{start}' duration:'$row{$col1}{$id}{duration}' status:'$row{$col1}{$id}{status}' type:'$row{$col1}{$id}{type}' input:'$row{$col1}{$id}{input}' output:'$row{$col1}{$id}{output}' device:'$row{$col1}{$id}{device}' rate:'$row{$col1}{$id}{rate}'");
					}
                }
        	}
		}
	}

#-------------------------------------------------------------------------------	
sub _getVrman_backup_job_details {
#-------------------------------------------------------------------------------
# Description:  Creating/Updating RRD File
# Input Parms:	Tablespace_Name
# Output Parms: None
#-------------------------------------------------------------------------------  
        my $self = shift;
		$self->ConnectDB();
        my @ergs;
        my $stmt =      q{
                        SELECT
                                command_id "id",
                                to_char(start_time,'rrrr.mm.dd hh24:mi') "start",
                                to_char(end_time,'rrrr.mm.dd hh24:mi') "end",
                                time_taken_display "duration",
                                status "status",
                                input_type "type",
                                output_device_type "device",
                                trim(output_bytes_per_sec_display) "rate",
                                trim(TO_CHAR(NVL(INPUT_BYTES / 1024 / 1024 / 1024, 0),'99999990.900')) "input",
                                trim(TO_CHAR(NVL(OUTPUT_BYTES / 1024 / 1024 / 1024, 0),'99999990.900')) "output"
                        FROM
                                v$rman_backup_job_details
                        WHERE
                                to_char(start_time,'rrrrmmddHH24MISS') >= to_char(sysdate-1,'rrrrmmddHH24MISS')
                        ORDER BY
                                SESSION_RECID
                        };
        my $sth  = $self->{DBH}->prepare($stmt);
        $sth->execute();
        my @colname=@{$sth->{NAME}};
        while (my @row = $sth->fetchrow_array()) {
                push(@ergs,\@row);
        }
        my %hash;
        foreach (@ergs){
                my %rman_status;
                my @aoh;
                for(my $i = 1; $i < 10; $i++){
                        $rman_status{$colname[$i]}=$_->[$i];
                }
        $hash{$colname[0]}{$_->[0]}=\%rman_status;
        }
        $sth->finish();
        $self->DisconnectDB();
        return %hash;
}	
1;
__END__
 
=pod
 
=head1 NAME
 
BACKUP_JOB_DETAILS.pm - monitors RMAN backup status 
 
=head1 SYNOPSIS
 
=head1 DESCRIPTION
 
=head1 PREREQUISITES
 
The follwing modules needs to be installed:
 
C<Log::Log4perl>
C<XML::Simple>
C<base qw(moniCA::DBH::Oracle)>
 
=head1 CONFIGURATION FILE
 
  By default
 
=head2 EXAMPLES
 
=head3 example of configuation file
 
Modul 	    MONITORING::DB::ORACLE::BACKUP_JOB_DETAILS
Logger		tbs-xe
LogLevel	INFO

<DBConnect>
	ConnectString	dbi:Oracle:SID=XE;HOST=localhost;port=1521
	dbuser		MONICA
	dbpw		monica
	description 	lokale oracle xe Testdatenbank
</DBConnect>

=cut
